package com.group.sliding.controller;

import com.group.sliding.domain.query.ItemPageQuery;
import com.group.sliding.domain.vo.ItemVO;
import com.group.sliding.domain.vo.SuggestVO;
import com.group.sliding.result.PageDTO;
import com.group.sliding.result.Result;
import com.group.sliding.service.IItemService;
import com.group.sliding.utils.ESUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@Api(tags = "搜索相关接口")
@RestController
@RequestMapping("/search")
@RequiredArgsConstructor
public class SearchController {

    private final IItemService itemService;
    private final ESUtils esUtils;

    @ApiOperation("搜索商品")
    @GetMapping("/list")
    public Result<PageDTO<ItemVO>> search(ItemPageQuery query) {
        log.info("搜索商品 {}", query);
        PageDTO<ItemVO> result = itemService.search(query);
        log.info("搜索商品结果 {}", result);
        return Result.success(result);
    }

    @ApiOperation("获取自动补全信息")
    @GetMapping("/suggest")
    public Result<List<SuggestVO>> querySuggestion(String prefix) {
        return Result.success(esUtils.suggest(prefix, 10));
    }
}
